home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / gfx / show / JPEGv42source.lha / GC / JPEG / jdmain.c < prev    next >
C/C++ Source or Header  |  1993-06-02  |  14KB  |  483 lines

  1. /*
  2.  * jdmain.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains a command-line user interface for the JPEG decompressor.
  9.  * It should work on any system with Unix- or MS-DOS-style command lines.
  10.  *
  11.  * Two different command line styles are permitted, depending on the
  12.  * compile-time switch TWO_FILE_COMMANDLINE:
  13.  *    djpeg [options]  inputfile outputfile
  14.  *    djpeg [options]  [inputfile]
  15.  * In the second style, output is always to standard output, which you'd
  16.  * normally redirect to a file or pipe to some other program.  Input is
  17.  * either from a named file or from standard input (typically redirected).
  18.  * The second style is convenient on Unix but is unhelpful on systems that
  19.  * don't support pipes.  Also, you MUST use the first style if your system
  20.  * doesn't do binary I/O to stdin/stdout.
  21.  */
  22.  
  23. #include "jinclude.h"
  24. #ifdef INCLUDES_ARE_ANSI
  25. #include <stdlib.h>        /* to declare exit() */
  26. #endif
  27. #include <ctype.h>        /* to declare isupper(), tolower() */
  28. #ifdef NEED_SIGNAL_CATCHER
  29. #include <signal.h>        /* to declare signal() */
  30. #endif
  31. #ifdef USE_SETMODE
  32. #include <fcntl.h>        /* to declare setmode() */
  33. #endif
  34.  
  35. #ifdef THINK_C
  36. #include <console.h>        /* command-line reader for Macintosh */
  37. #endif
  38.  
  39. #ifdef DONT_USE_B_MODE        /* define mode parameters for fopen() */
  40. #define READ_BINARY    "r"
  41. #define WRITE_BINARY    "w"
  42. #else
  43. #define READ_BINARY    "rb"
  44. #define WRITE_BINARY    "wb"
  45. #endif
  46.  
  47. #ifndef EXIT_FAILURE        /* define exit() codes if not provided */
  48. #define EXIT_FAILURE  1
  49. #endif
  50. #ifndef EXIT_SUCCESS
  51. #ifdef VMS
  52. #define EXIT_SUCCESS  1        /* VMS is very nonstandard */
  53. #else
  54. #define EXIT_SUCCESS  0
  55. #endif
  56. #endif
  57.  
  58.  
  59. #include "jversion.h"        /* for version message */
  60.  
  61.  
  62. /*
  63.  * This list defines the known output image formats
  64.  * (not all of which need be supported by a given version).
  65.  * You can change the default output format by defining DEFAULT_FMT;
  66.  * indeed, you had better do so if you undefine PPM_SUPPORTED.
  67.  */
  68.  
  69. typedef enum {
  70.     FMT_GIF,        /* GIF format */
  71.     FMT_PPM,        /* PPM/PGM (PBMPLUS formats) */
  72.     FMT_RLE,        /* RLE format */
  73.     FMT_TARGA,        /* Targa format */
  74.     FMT_TIFF,        /* TIFF format */
  75.     FMT_RETINA
  76. } IMAGE_FORMATS;
  77.  
  78. #ifndef DEFAULT_FMT        /* so can override from CFLAGS in Makefile */
  79. #define DEFAULT_FMT    FMT_RETINA
  80. #endif
  81.  
  82. static IMAGE_FORMATS requested_fmt;
  83.  
  84.  
  85. /*
  86.  * This routine gets control after the input file header has been read.
  87.  * It must determine what output file format is to be written,
  88.  * and make any other decompression parameter changes that are desirable.
  89.  */
  90.  
  91. METHODDEF void
  92. d_ui_method_selection (decompress_info_ptr cinfo)
  93. {
  94.   /* if grayscale or CMYK input, force similar output; */
  95.   /* else leave the output colorspace as set by options. */
  96.   if (cinfo->jpeg_color_space == CS_GRAYSCALE)
  97.     cinfo->out_color_space = CS_GRAYSCALE;
  98.   else if (cinfo->jpeg_color_space == CS_CMYK)
  99.     cinfo->out_color_space = CS_CMYK;
  100.  
  101.   /* select output file format */
  102.   /* Note: jselwxxx routine may make additional parameter changes,
  103.    * such as forcing color quantization if it's a colormapped format.
  104.    */
  105.   switch (requested_fmt) {
  106. #ifdef GIF_SUPPORTED
  107.   case FMT_GIF:
  108.     jselwgif(cinfo);
  109.     break;
  110. #endif
  111. #ifdef PPM_SUPPORTED
  112.   case FMT_PPM:
  113.     jselwppm(cinfo);
  114.     break;
  115. #endif
  116. #ifdef RLE_SUPPORTED
  117.   case FMT_RLE:
  118.     jselwrle(cinfo);
  119.     break;
  120. #endif
  121. #ifdef TARGA_SUPPORTED
  122.   case FMT_TARGA:
  123.     jselwtarga(cinfo);
  124.     break;
  125. #endif
  126.   case FMT_RETINA:
  127.     jselwretina(cinfo);
  128.     break;
  129.   default:
  130.     ERREXIT(cinfo->emethods, "Unsupported output file format");
  131.     break;
  132.   }
  133. }
  134.  
  135.  
  136. /*
  137.  * Signal catcher to ensure that temporary files are removed before aborting.
  138.  * NB: for Amiga Manx C this is actually a global routine named _abort();
  139.  * see -Dsignal_catcher=_abort in CFLAGS.  Talk about bogus...
  140.  */
  141.  
  142. #ifdef NEED_SIGNAL_CATCHER
  143.  
  144. static external_methods_ptr emethods; /* for access to free_all */
  145.  
  146. GLOBAL void
  147. signal_catcher (int signum)
  148. {
  149.   if (emethods != NULL) {
  150.     emethods->trace_level = 0;    /* turn off trace output */
  151.     (*emethods->free_all) ();    /* clean up memory allocation & temp files */
  152.   }
  153.   exit(EXIT_FAILURE);
  154. }
  155.  
  156. #endif
  157.  
  158.  
  159. /*
  160.  * Optional routine to display a percent-done figure on stderr.
  161.  * See jddeflts.c for explanation of the information used.
  162.  */
  163.  
  164. #ifdef PROGRESS_REPORT
  165.  
  166. METHODDEF void
  167. progress_monitor (decompress_info_ptr cinfo, long loopcounter, long looplimit)
  168. {
  169.   if (cinfo->total_passes > 1) {
  170.     fprintf(stderr, "\rPass %d/%d: %3d%% ",
  171.         cinfo->completed_passes+1, cinfo->total_passes,
  172.         (int) (loopcounter*100L/looplimit));
  173.   } else {
  174.     fprintf(stderr, "\r %3d%% ",
  175.         (int) (loopcounter*100L/looplimit));
  176.   }
  177.   fflush(stderr);
  178. }
  179.  
  180. #endif
  181.  
  182.  
  183. /*
  184.  * Argument-parsing code.
  185.  * The switch parser is designed to be useful with DOS-style command line
  186.  * syntax, ie, intermixed switches and file names, where only the switches
  187.  * to the left of a given file name affect processing of that file.
  188.  * The main program in this file doesn't actually use this capability...
  189.  */
  190.  
  191.  
  192. static char * progname;        /* program name for error messages */
  193.  
  194.  
  195. LOCAL void
  196. usage (void)
  197. /* complain about bad command line */
  198. {
  199.   fprintf(stderr, "usage: %s [switches] ", progname);
  200. #ifdef TWO_FILE_COMMANDLINE
  201.   fprintf(stderr, "inputfile outputfile\n");
  202. #else
  203.   fprintf(stderr, "[inputfile]\n");
  204. #endif
  205.  
  206.   fprintf(stderr, "Switches (names may be abbreviated):\n");
  207.   fprintf(stderr, "  -colors N      Reduce image to no more than N colors\n");
  208. #ifdef GIF_SUPPORTED
  209.   fprintf(stderr, "  -gif           Select GIF output format\n");
  210. #endif
  211. #ifdef PPM_SUPPORTED
  212.   fprintf(stderr, "  -pnm           Select PBMPLUS (PPM/PGM) output format\n");
  213. #endif
  214.   fprintf(stderr, "  -quantize N    Same as -colors N\n");
  215. #ifdef RLE_SUPPORTED
  216.   fprintf(stderr, "  -rle           Select Utah RLE output format\n");
  217. #endif
  218. #ifdef TARGA_SUPPORTED
  219.   fprintf(stderr, "  -targa         Select Targa output format\n");
  220. #endif
  221.   fprintf(stderr, "  -retina        Display picture on retina (default)\n");
  222.   fprintf(stderr, "Switches for advanced users:\n");
  223. #ifdef BLOCK_SMOOTHING_SUPPORTED
  224.   fprintf(stderr, "  -blocksmooth   Apply cross-block smoothing\n");
  225. #endif
  226.   fprintf(stderr, "  -grayscale     Force grayscale output\n");
  227.   fprintf(stderr, "  -nodither      Don't use dithering in quantization\n");
  228. #ifdef QUANT_1PASS_SUPPORTED
  229.   fprintf(stderr, "  -onepass       Use 1-pass quantization (fast, low quality)\n");
  230. #endif
  231.   fprintf(stderr, "  -maxmemory N   Maximum memory to use (in kbytes)\n");
  232.   fprintf(stderr, "  -verbose  or  -debug   Emit debug output\n");
  233.   exit(EXIT_FAILURE);
  234. }
  235.  
  236.  
  237. LOCAL boolean
  238. keymatch (char * arg, const char * keyword, int minchars)
  239. /* Case-insensitive matching of (possibly abbreviated) keyword switches. */
  240. /* keyword is the constant keyword (must be lower case already), */
  241. /* minchars is length of minimum legal abbreviation. */
  242. {
  243.   register int ca, ck;
  244.   register int nmatched = 0;
  245.  
  246.   while ((ca = *arg++) != '\0') {
  247.     if ((ck = *keyword++) == '\0')
  248.       return FALSE;        /* arg longer than keyword, no good */
  249.     if (isupper(ca))        /* force arg to lcase (assume ck is already) */
  250.       ca = tolower(ca);
  251.     if (ca != ck)
  252.       return FALSE;        /* no good */
  253.     nmatched++;            /* count matched characters */
  254.   }
  255.   /* reached end of argument; fail if it's too short for unique abbrev */
  256.   if (nmatched < minchars)
  257.     return FALSE;
  258.   return TRUE;            /* A-OK */
  259. }
  260.  
  261.  
  262. LOCAL int
  263. parse_switches (decompress_info_ptr cinfo, int last_file_arg_seen,
  264.         int argc, char **argv)
  265. /* Initialize cinfo with default switch settings, then parse option switches.
  266.  * Returns argv[] index of first file-name argument (== argc if none).
  267.  * Any file names with indexes <= last_file_arg_seen are ignored;
  268.  * they have presumably been processed in a previous iteration.
  269.  * (Pass 0 for last_file_arg_seen on the first or only iteration.)
  270.  */
  271. {
  272.   int argn;
  273.   char * arg;
  274.  
  275.   /* (Re-)initialize the system-dependent error and memory managers. */
  276.   jselerror(cinfo->emethods);    /* error/trace message routines */
  277.   jselmemmgr(cinfo->emethods);    /* memory allocation routines */
  278.   cinfo->methods->d_ui_method_selection = d_ui_method_selection;
  279.  
  280.   /* Now OK to enable signal catcher. */
  281. #ifdef NEED_SIGNAL_CATCHER
  282.   emethods = cinfo->emethods;
  283. #endif
  284.  
  285.   /* Set up default JPEG parameters. */
  286.   j_d_defaults(cinfo, TRUE);
  287.   requested_fmt = DEFAULT_FMT;    /* set default output file format */
  288.  
  289.   /* Scan command line options, adjust parameters */
  290.  
  291.   for (argn = 1; argn < argc; argn++) {
  292.     arg = argv[argn];
  293.     if (*arg != '-') {
  294.       /* Not a switch, must be a file name argument */
  295.       if (argn <= last_file_arg_seen)
  296.     continue;        /* ignore it if previously processed */
  297.       break;            /* else done parsing switches */
  298.     }
  299.     arg++;            /* advance past switch marker character */
  300.  
  301.     if (keymatch(arg, "blocksmooth", 1)) {
  302.       /* Enable cross-block smoothing. */
  303.       cinfo->do_block_smoothing = TRUE;
  304.  
  305.     } else if (keymatch(arg, "colors", 1) || keymatch(arg, "colours", 1) ||
  306.            keymatch(arg, "quantize", 1) || keymatch(arg, "quantise", 1)) {
  307.       /* Do color quantization. */
  308.       int val;
  309.  
  310.       if (++argn >= argc)    /* advance to next argument */
  311.     usage();
  312.       if (sscanf(argv[argn], "%d", &val) != 1)
  313.     usage();
  314.       cinfo->desired_number_of_colors = val;
  315.       cinfo->quantize_colors = TRUE;
  316.  
  317.     } else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
  318.       /* Enable debug printouts. */
  319.       /* On first -d, print version identification */
  320.       if (last_file_arg_seen == 0 && cinfo->emethods->trace_level == 0)
  321.     fprintf(stderr, "Independent JPEG Group's DJPEG, version %s\n%s\n",
  322.         JVERSION, JCOPYRIGHT);
  323.       cinfo->emethods->trace_level++;
  324.  
  325.     } else if (keymatch(arg, "gif", 1)) {
  326.       /* GIF output format. */
  327.       requested_fmt = FMT_GIF;
  328.  
  329.     } else if (keymatch(arg, "grayscale", 2) || keymatch(arg, "greyscale",2)) {
  330.       /* Force monochrome output. */
  331.       cinfo->out_color_space = CS_GRAYSCALE;
  332.  
  333.     } else if (keymatch(arg, "maxmemory", 1)) {
  334.       /* Maximum memory in Kb (or Mb with 'm'). */
  335.       long lval;
  336.       char ch = 'x';
  337.  
  338.       if (++argn >= argc)    /* advance to next argument */
  339.     usage();
  340.       if (sscanf(argv[argn], "%ld%c", &lval, &ch) < 1)
  341.     usage();
  342.       if (ch == 'm' || ch == 'M')
  343.     lval *= 1000L;
  344.       cinfo->emethods->max_memory_to_use = lval * 1000L;
  345.  
  346.     } else if (keymatch(arg, "nodither", 3)) {
  347.       /* Suppress dithering in color quantization. */
  348.       cinfo->use_dithering = FALSE;
  349.  
  350.     } else if (keymatch(arg, "onepass", 1)) {
  351.       /* Use fast one-pass quantization. */
  352.       cinfo->two_pass_quantize = FALSE;
  353.  
  354.     } else if (keymatch(arg, "pnm", 1)) {
  355.       /* PPM/PGM output format. */
  356.       requested_fmt = FMT_PPM;
  357.  
  358.     } else if (keymatch(arg, "retina", 1)) {
  359.       /* retina display */
  360.       requested_fmt = FMT_RETINA;
  361.  
  362.     } else if (keymatch(arg, "rle", 1)) {
  363.       /* RLE output format. */
  364.       requested_fmt = FMT_RLE;
  365.  
  366.     } else if (keymatch(arg, "targa", 1)) {
  367.       /* Targa output format. */
  368.       requested_fmt = FMT_TARGA;
  369.  
  370.     } else {
  371.       usage();            /* bogus switch */
  372.     }
  373.   }
  374.  
  375.   return argn;            /* return index of next arg (file name) */
  376. }
  377.  
  378.  
  379. /*
  380.  * The main program.
  381.  */
  382.  
  383. GLOBAL int
  384. main (int argc, char **argv)
  385. {
  386.   struct Decompress_info_struct cinfo;
  387.   struct Decompress_methods_struct dc_methods;
  388.   struct External_methods_struct e_methods;
  389.   int file_index;
  390.  
  391.   /* On Mac, fetch a command line. */
  392. #ifdef THINK_C
  393.   argc = ccommand(&argv);
  394. #endif
  395.  
  396.   progname = argv[0];
  397.  
  398.   /* Set up links to method structures. */
  399.   cinfo.methods = &dc_methods;
  400.   cinfo.emethods = &e_methods;
  401.  
  402.   /* Install, but don't yet enable signal catcher. */
  403. #ifdef NEED_SIGNAL_CATCHER
  404.   emethods = NULL;
  405.   signal(SIGINT, signal_catcher);
  406. #ifdef SIGTERM            /* not all systems have SIGTERM */
  407.   signal(SIGTERM, signal_catcher);
  408. #endif
  409. #endif
  410.  
  411.   /* Scan command line: set up compression parameters, input & output files. */
  412.  
  413.   file_index = parse_switches(&cinfo, 0, argc, argv);
  414.  
  415. #ifdef TWO_FILE_COMMANDLINE
  416.  
  417.   if (file_index != argc-2) {
  418.     fprintf(stderr, "%s: must name one input and one output file\n", progname);
  419.     usage();
  420.   }
  421.   if ((cinfo.input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
  422.     fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
  423.     exit(EXIT_FAILURE);
  424.   }
  425.   if ((cinfo.output_file = fopen(argv[file_index+1], WRITE_BINARY)) == NULL) {
  426.     fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index+1]);
  427.     exit(EXIT_FAILURE);
  428.   }
  429.  
  430. #else /* not TWO_FILE_COMMANDLINE -- use Unix style */
  431.  
  432.   cinfo.input_file = stdin;    /* default input file */
  433.   cinfo.output_file = stdout;    /* always the output file */
  434.  
  435. #ifdef USE_SETMODE        /* need to hack file mode? */
  436.   setmode(fileno(stdin), O_BINARY);
  437.   setmode(fileno(stdout), O_BINARY);
  438. #endif
  439.  
  440.   if (file_index < argc-1) {
  441.     fprintf(stderr, "%s: only one input file\n", progname);
  442.     usage();
  443.   }
  444.   if (file_index < argc) {
  445.     if ((cinfo.input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
  446.       fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
  447.       exit(EXIT_FAILURE);
  448.     }
  449.   }
  450.  
  451. #endif /* TWO_FILE_COMMANDLINE */
  452.   
  453.   /* Set up to read a JFIF or baseline-JPEG file. */
  454.   /* A smarter UI would inspect the first few bytes of the input file */
  455.   /* to determine its type. */
  456. #ifdef JFIF_SUPPORTED
  457.   jselrjfif(&cinfo);
  458. #else
  459.   You shoulda defined JFIF_SUPPORTED.   /* deliberate syntax error */
  460. #endif
  461.  
  462. #ifdef PROGRESS_REPORT
  463.   /* Start up progress display, unless trace output is on */
  464.   if (e_methods.trace_level == 0)
  465.     dc_methods.progress_monitor = progress_monitor;
  466. #endif
  467.  
  468.   /* Do it to it! */
  469.   jpeg_decompress(&cinfo);
  470.  
  471. #ifdef PROGRESS_REPORT
  472.   /* Clear away progress display */
  473.   if (e_methods.trace_level == 0) {
  474.     fprintf(stderr, "\r                \r");
  475.     fflush(stderr);
  476.   }
  477. #endif
  478.  
  479.   /* All done. */
  480.   exit(EXIT_SUCCESS);
  481.   return 0;            /* suppress no-return-value warnings */
  482. }
  483.